package com.dataextend.controller;

import com.dataextend.entity.*;
import com.dataextend.service.*;
import com.dataextend.utils.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "角色表接口")
@RestController
@RequestMapping("/api/role")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class RootRoleController extends RootBaseApiController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation("添加角色")
    @RequiresPermissions("role:add")
    @PostMapping("/add")
    public Map<String, Object> add(@RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "memo", required = false) String memo)
    {
        RoleEntity roleEntity = new RoleEntity()
                .setName(name)
                .setMemo(memo)
                .setStatus(0)
                .setCreateTime(new DateUtils().getDateTime())
                .setUpdateTime(new DateUtils().getDateTime());

        if (roleService.add(roleEntity)==0) {
            return resp(406,"添加失败");
        } else {
            return resp(200, "添加成功", roleEntity);
        }
    }

    @ApiOperation("修改角色信息")
    @RequiresPermissions("role:update")
    @PostMapping("/update")
    public Map<String, Object> update(@RequestParam(value = "id", required = false) Long id,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "memo", required = false) String memo,
                                      @RequestParam(value = "status", required = false) Integer status) {
        RoleEntity roleEntity = new RoleEntity()
                .setId(id)
                .setName(name)
                .setMemo(memo)
                .setStatus(status)
                .setUpdateTime(new DateUtils().getDateTime());

        return resp(200, "修改成功", roleService.update(roleEntity));
    }

    @ApiOperation("删除角色")
    @RequiresPermissions("role:delete")
    @PostMapping("/delete")
    public Map<String, Object> deleteById(@RequestParam(value = "id", required = false) Long id) {
        // 删除角色前先删除角色权限
        List<RolePermissionEntity> rolePermissionEntityList = rolePermissionService.list(new RolePermissionEntity().setRoleId(id));
        for (RolePermissionEntity rp: rolePermissionEntityList) {
            rolePermissionService.delete(rp.getId());
        }
        roleService.delete(id);
        return resp(200, "删除成功");
    }

    @ApiOperation("根据id查询角色信息")
    @GetMapping("/query")
    public Map<String, Object> queryById(@RequestParam(value = "id", required = false) Long id) {
        return resp(200, "查询成功", roleService.queryById(id));
    }

    @ApiOperation("查询所有角色信息")
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam(value = "pageNum", required = false) int pageNum,
                                    @RequestParam(value = "pageSize", required = false) int pageSize,
                                    @RequestParam(value = "id", required = false) Long id,
                                    @RequestParam(value = "name", required = false) String name,
                                    @RequestParam(value = "memo", required = false) String memo) {
        RoleEntity roleEntity = new RoleEntity()
                .setId(id)
                .setName(name)
                .setMemo(memo);

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id asc");
        List<RoleEntity> list = roleService.list(roleEntity);
        PageInfo<RoleEntity> pageInfo =  new PageInfo<RoleEntity>(list);
        return resp(200, "查询所有成功", pageInfo);
    }

    @ApiOperation("查询所有角色信息")
    @GetMapping("/list/all")
    public Map<String, Object> list() {
        return resp(200, "查询所有成功", roleService.list(new RoleEntity()));
    }
}
